package concurrent.lock.locksupport;

/**
 * wait/notify必须在同步线程代码中，否则会包java.lang.IllegalMonitorStateException
 * <p>
 * wait/notify的顺序不能颠倒，否则线程被wait的线程无法被唤醒了
 */
public class WaitNotifyDemo {

	private static Object o = new Object();

	public static void main(String[] args) {

		new Thread(() -> {
			synchronized (o) {
				System.out.println(Thread.currentThread().getName() + " come in.");
				try {
					o.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + " 换醒.");
			}
		}, "线程1").start();

		new Thread(() -> {
			o.notify();
			synchronized (o) {
				System.out.println(Thread.currentThread().getName() + " 通知.");

			}
		}, "线程2").start();


	}

}
